<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class User_Model extends Model {

    public function __construct() {
        parent::__construct();
    }

    public function userList() {

        $sth = $this->db->prepare("select user_id,nip,username,role from users");

//         $sth = $this->db->prepare("select user_id,nip,nama,username,role from users");

        $sth->execute();

        $data = $sth->fetchAll();

        return $data;

//        print_r($data);
    }

    public function addUser($data) {

        $sth = $this->db->prepare
                (
                "INSERT INTO users 
                (username,password,role)
                VALUES 
                (:username,:password,:role)"
        );
        $sth->execute(array(
            ':username' => $data['username'],
            ':password' => Hash::create('md5', $data['password'], HASH_PASSWORD_KEY),
            ':role' => $data['role']
        ));
    }

    public function deleteUser($id) {

        $sth = $this->db->prepare("DELETE from users where user_id = :id");

        $sth->execute(array(
            ':id' => $id
        ));
    }

    public function editUser($id) {

        $sth = $this->db->prepare("select * from users where user_id = :id");

        $sth->execute(array(
            ':id' => $id
        ));

        $data = $sth->fetch();

        return $data;
    }

    public function editSave($data) {

        $sth = $this->db->prepare("UPDATE users SET 
                username = :username,
                password = :password,
                role = :role
                WHERE 
                user_id = :id
                ");

        $sth->execute(array(
            ':username' => $data['username'],
            ':password' => Hash::create('md5', $data['password'], HASH_PASSWORD_KEY),
            ':role' => $data['role'],
            ':id' => $data['id']
        ));
    }

    public function page() {
        $item_page = 10;

        $sth = $this->db->prepare("select user_id from users");

        $sth->execute();

        $row = $sth->rowCount();

        $page = ceil($row / $item_page);

        return $page;
    }

    public function user_detail() {
        $nrp = session::get('nrp');
        $sth = $this->db->prepare("select * from user_detail where nrp = :nrp ");

        $sth->execute(array(
            ':nrp' => $nrp
        ));

        $data = $sth->fetch();
        return $data;
    }

    public function makul_user() {
        $nrp = session::get('nrp');
        $sth = $this->db->prepare("select kd_univ,kd_jurusan from user_detail where nrp = :nrp ");
        $sth->execute(array(
            ':nrp' => $nrp
        ));

        $data = $sth->fetch();

        $kd_univ = $data['kd_univ'];
        $kd_jurusan = $data['kd_jurusan'];

        $sth2 = $this->db->prepare("select kd_makul from makul 
            where 
            kd_univ = :kd_univ 
            and 
            kd_jurusan = :kd_jurusan");

        $sth2->execute(array(
            ':kd_univ' => $kd_univ,
            ':kd_jurusan' => $kd_jurusan
        ));

        $data2 = $sth2->fetchAll();
        return $data2;
    }

}

?>
